package com.shinji.tratu;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class WordDatabase {
	private static WordDatabase database;
	private WordDatabaseHelper databaseHelper;
	private SQLiteDatabase readableDatabase, writableDatabase;
	private String[] allColumns = { Constants.WORD_ID, Constants.WORD_KEY,
			Constants.WORD_SEARCH_SUCCEED, Constants.WORD_DIC_TYPE,
			Constants.WORD_SEARCH_RESULT, Constants.WORD_ON_HANDBOOK,
			Constants.WORD_DIC_INDEX, Constants.WORD_AUDIO_SERVER_LINK };

	public static WordDatabase getInstance(Context ctx) {
		if (database == null) {
			database = new WordDatabase(ctx);
		}
		return database;
	}

	private WordDatabase(Context context) {
		databaseHelper = new WordDatabaseHelper(context,
				Constants.DATABASE_NAME, null, Constants.DATABASE_VERSION);
		readableDatabase = databaseHelper.getReadableDatabase();
		writableDatabase  = databaseHelper.getWritableDatabase();
	}

	/**
	 * tao mot ban ghi moi trong bang
	 * 
	 * @param wordRecord
	 * @return
	 */
	public long createRecord(Word wordRecord) {
		ContentValues values = new ContentValues();
		values.put(allColumns[1], wordRecord.getKey());
		values.put(allColumns[2], wordRecord.isSearchSucceed());
		values.put(allColumns[3], wordRecord.getDicType());
		values.put(allColumns[4], wordRecord.getSearchResult());
		values.put(allColumns[5], wordRecord.isOnHandbook());
		values.put(allColumns[6], wordRecord.getDicIndex());
		values.put(allColumns[7], wordRecord.getServerAudioLink());
		long insertId = writableDatabase.insert(
				Constants.WORD_TABLE, null, values);
		wordRecord.setId(insertId);
		return insertId;
	}

	public void deleteAWord(long id) {
		writableDatabase.delete(Constants.WORD_TABLE,
				allColumns[0] + " = " + id, null);
	}
	
	public void deleteAll() {
		writableDatabase.delete(Constants.WORD_TABLE,null, null);
	}


	/**
	 * map cac gia tri lay duoc tu trong bang vao doi tuong Constants.WORD_ID,
	 * Constants.WORD_KEY, Constants.WORD_SEARCH_SUCCEED,
	 * Constants.WORD_DIC_TYPE, Constants.WORD_SEARCH_RESULT
	 * 
	 * @param cursor
	 * @return
	 */
	private Word cursorToObject(Cursor cursor) {
		Word word = new Word();
		String key = cursor.getString(1);
		boolean searchSucceed = "1".equals(cursor.getInt(2)) ? true : false;
		String dicType = cursor.getString(3);
		String searchResult = cursor.getString(4);
		boolean onHandbook = "1".equals(cursor.getInt(5)) ? true : false;
		int dicIndex = cursor.getInt(6);
		String audioServerLink = cursor.getString(7);
		word.setId(cursor.getLong(0));
		word.setKey(key);
		word.setSearchSucceed(searchSucceed);
		word.setDicType(dicType);
		word.setSearchResult(searchResult);
		word.setOnHandbook(onHandbook);
		word.setDicIndex(dicIndex);
		word.setServerAudioLink(audioServerLink);
		return word;
	}

	public ArrayList<Word> getAllRecords() {
		ArrayList<Word> listWords = new ArrayList<Word>();

		Cursor cursor = readableDatabase.query(
				Constants.WORD_TABLE, allColumns, null, null, null, null, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Word word = cursorToObject(cursor);
			listWords.add(word);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return listWords;
	}

	public Word findWordById(int id) {

		Cursor cursor = readableDatabase.query(
				Constants.WORD_TABLE, allColumns,
				Constants.WORD_ID + " = " + id, null, null, null, null);

		cursor.moveToFirst();
		Word word = null;
		while (!cursor.isAfterLast()) {
			word = cursorToObject(cursor);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return word;
	}

}
